import Ember from 'ember';

export default Ember.Controller.extend({
  detailEdit:false,
  actions:{
    modifyConf(drugConf){
      this.set('showModify',true);
      this.set('curDrugConf',drugConf);
    },
    close(){
      this.set('showModify',false);
      this.get("drugConf").rollbackAttributes();
    },
    //保存
    saveConf(conf) {
      let _self = this;
      let curDrugConf = this.get("curDrugConf");
      curDrugConf.save().then(function(){
        _self.set('showModify',false);
        // conf.set('choosed',false);
      },function(err){
        _self.get("drugConf").rollbackAttributes();
      });
    },
    chooseConf(conf){
      // this.get('confList').forEach(function(conf){
      //   conf.set('choosed',false);
      // });
      return;
      // conf.set('choosed',true);
    },
    //编辑按钮
    detailEditClick(){
      this.set('detailEdit',true);
    },
    //取消
    detailCancel(){
      this.set('detailEdit',false);
    },
    toCreateRecord(){
      let _self = this;
      let customerdrugreport = this.get('store').createRecord('customerdrugreport', {});
      App.lookup('controller:business.mainpage').openPopTip("正在生成记录");
      customerdrugreport.save().then(function(customerdrugreport){
        App.lookup('controller:business.mainpage').showPopTip("操作成功");
        var route = App.lookup('route:business.mainpage.parameter-setting');
        App.lookup('controller:business.mainpage').refreshPage(route);
      },function(err){
        console.log("save err!");
        console.log("err:",err);
        let error = err.errors[0];
        if (error.code === "15") {
          App.lookup("controller:business.mainpage").showAlert("今天的统计记录已经存在");
          App.lookup('controller:business.mainpage').showPopTip("操作失败",false);
        }
      });
    },
    toDetailPage(report){
      let _self = this;
      this.set("curReport",report);
      _self.set('lookDrugModel',true);
      let reportId = report.get("id");
      _self.set("itemList",null);
      this.store.query("customerdrugreportitem",{
        filter: {
          report:{
            id:reportId
          }
        }
      }).then(function(itemList){
        _self.set("itemList",itemList);
      });
    },
    //弹层取消
    invitation(){
      this.set('lookDrugModel',false);
    },
    //打印
    printImg(){
      $("<iframe ></iframe>", { id: 'myiframe' }).bind('load', function(event) {
          if (!this.contentWindow) {
              return;
          }
          var scripWidthCode = document.createElement('script');
          scripWidthCode.type ='text/javascript';
          scripWidthCode.innerText = 'var ad_unit="123";';
          this.contentWindow.document.getElementsByTagName('head')[0].appendChild(scripWidthCode);
          var scripWidthSrc = document.createElement('script');
          scripWidthSrc.type ='text/javascript';
          scripWidthSrc.src = 'http://abc.com/abc.js';
          var cssSrc = document.createElement('link');
          cssSrc.rel ='stylesheet';
          cssSrc.href = 'assets/images/print.css';
          this.contentWindow.document.getElementsByTagName('head')[0].appendChild(scripWidthSrc);
          this.contentWindow.document.getElementsByTagName('head')[0].appendChild(cssSrc);
          setTimeout(function(){
            // let bdhtml=window.document.body.innerHTML;
            // let sprnstr="<!--startprint-->";
            // let eprnstr="<!--endprint-->";
            // let prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
            let prnhtml=$("body div[name='printbegin']").html();
            console.log("prnhtml is:" + prnhtml);
            $('#myiframe').contents().find('body').html(prnhtml);
            document.getElementById('myiframe').contentWindow.print();
            $('#myiframe').remove();
          },1000);
      }).appendTo('body');
    },
  }
});
